<template>
  <div>
    <div class="paymentBox">
      <p class="moneyTitle">{{ $t("balance") }}</p>
      <div class="money">
        <div class="moneyNum">
          <span>{{ formatMoney(total_balance, true) || "--" }}</span>
          <span>{{ coin_name }}</span>
        </div>
        <div>
          <el-button
            type="primary"
            class="moneyBtn"
            @click="isShowSendDrawer = true"
            >{{ $t("send") }}</el-button
          >
          <el-button
            type="primary"
            class="moneyBtn"
            @click="isShowReceiveDrawer = true"
            >{{ $t("receive") }}</el-button
          >
        </div>
      </div>
      <el-divider />
      <div class="moneyInfo">
        <div class="moneyInfoItem">
          <span>{{ $t("total") }}: </span>
          <span
            >{{ formatMoney(available, true) || "--" }} {{ coin_name }}</span
          >
        </div>
        <div class="moneyInfoItem">
          <span>{{ $t("frozen") }}: </span>
          <span>{{ formatMoney(freeze, true) || "--" }} {{ coin_name }}</span>
        </div>
      </div>
    </div>
    <div class="record">
      <div class="recordTop">
        <h4>{{ $t("record") }}</h4>
        <p class="more" @click="$router.push('/record')">
          {{ $t("more") }}
        </p>
      </div>
      <el-table
        :data="list"
        style="width: 100%"
        :empty-text="$t('noData')"
        v-loading="loading"
      >
        <el-table-column prop="bill_no" :label="$t('orderNo')" align="center">
        </el-table-column>
        <el-table-column prop="status" :label="$t('status')" align="center">
          <template slot-scope="scope">
            <span class="text-success" v-if="scope.row.status == 1">
              {{ $t("success") }}</span
            >
            <span class="text-info" v-if="scope.row.status == 0">
              {{ $t("pending") }}</span
            >
            <span class="text-danger" v-if="scope.row.status == 2">
              {{ $t("error") }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="trade_type"
          :label="$t('tradeType')"
          align="center"
          width="180"
        >
        </el-table-column>

        <el-table-column prop="create_time" :label="$t('time')" align="center">
          <template slot-scope="scope">
            {{ getTimes(scope.row.create_time) }}
          </template>
        </el-table-column>
        <el-table-column
          prop="coin_name"
          :label="$t('currency')"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="amount" :label="$t('count')" align="center">
          <template slot-scope="scope">
            <span :class="scope.row.sg == '+' ? 'green' : 'red'">
              {{ scope.row.sg }} {{ scope.row.amount }}
            </span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('operate')" align="center">
          <template slot-scope="scope">
            <el-button
              class="tableBtn"
              size="small"
              @click="goDetail(scope.row)"
              >{{ $t("view") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <walletDetailDrawer
      :isShowOrderDetail="isShowOrderDetail"
      :coinData="coinData"
      from="records"
      :order_no="currentOrderNo"
      @close="clearNo"
    />
    <walletSendDrawer
      :show="isShowSendDrawer"
      :coin_name="coin_name"
      :available="Number(available)"
      @close="isShowSendDrawer = false"
      @refresh="refresh"
    />
    <walletReceiveDrawer
      :show="isShowReceiveDrawer"
      @close="isShowReceiveDrawer = false"
    />
  </div>
</template>
<script>
import { formatMoney, getTimes } from "@/utils/common";
import walletDetailDrawer from "./components/walletDetailDrawer.vue";
import walletSendDrawer from "./components/walletSendDrawer.vue";
import walletReceiveDrawer from "./components/walletReceiveDrawer.vue";
// import QRCode from "qrcodejs2";
import { copyText } from "@/utils/common";
import { httpWalletBalance, httpInfo, httpWalletBills } from "@/http/api";
export default {
  name: "wallet",
  components: {
    walletDetailDrawer,
    walletSendDrawer,
    walletReceiveDrawer,
  },
  data() {
    return {
      payWayTitle: "",
      chain_name: "",
      amount: "",
      address: "",
      available: 0,
      total_balance: 0,
      freeze: 0,
      coin_name: "USDT",
      is_wallet: 0, // 1开通 0 没有
      list: [],
      loading: false,
      form: { bind_email: "" },
      rules: {
        bind_email: [
          {
            required: true,
            message: this.$t("walletEmailTip"),
            trigger: "change",
          },
        ],
        bind_password: [
          { required: true, validator: this.checkPwd, trigger: "blur" },
        ],
      },
      isShowSendDrawer: false,
      isShowReceiveDrawer: false,
      isShowOrderDetail: false,
      currentOrderNo: "",
      coinData: {},
    };
  },
  created() {
    // this.init();
    this.getHttpInfo();
  },
  methods: {
    formatMoney,
    getTimes,
    checkPwd(rule, value, callback) {
      if (!value) {
        callback(new Error(this.$t("walletPsdTip")));
      } else {
        const checkPwdFormat = new RegExp(/^[0-9a-zA-Z]{6,16}$/);
        if (!checkPwdFormat.test(value)) {
          callback(new Error(this.$t("walletPsdTip1")));
          return;
        }
        callback();
      }
    },
    copy() {
      copyText(this.address);
      this.$message.success(this.$t("copySuccess"));
    },
    refresh() {
      this.getBalance();
    },
    getBalance() {
      httpWalletBalance().then((res) => {
        this.available = res.data.available || 0;
        this.total_balance = res.data.total_balance || 0;
        this.freeze = res.data.freeze || 0;
        this.coin_name = res.data.coin_name || "USDT";
      });
    },
    async getHttpInfo() {
      try {
        this.loading = true;
        let res = await httpInfo();
        if (res.code == 0) {
          this.is_wallet = res.data?.is_wallet || 0;
          this.form.bind_email = res.data?.account_name || "";
          if (this.is_wallet == 1) {
            await this.getBalance();
            await this.getRecord();
            // await this.getAddress();
          }
        }
        this.loading = false;
      } catch {
        this.loading = false;
      }
    },
    goDetail(row) {
      this.coinData = row;
      this.currentOrderNo = row.order_no;
      this.isShowOrderDetail = true;
    },
    clearNo() {
      this.currentOrderNo = "";
      this.isShowOrderDetail = false;
    },

    async getRecord() {
      let res = await httpWalletBills({ page: 1, pagesize: 10 });
      this.list = res.data?.list || [];
    },
    // async getAddress() {
    //   let res = await httpWalletAddress();
    //   const { chain_name, address } = res.data;
    //   this.chain_name = chain_name;
    //   this.address = address;
    //   //   this.$nextTick(() => {
    //   //     new QRCode(this.$refs.cbcCode, {
    //   //       text: address,
    //   //       width: 200,
    //   //       height: 200,
    //   //       colorDark: "#000000",
    //   //       colorLight: "#ffffff",
    //   //       correctLevel: QRCode.CorrectLevel.H,
    //   //     });
    //   //   });
    // },
    // openWallet(formName) {
    //   this.$refs[formName].validate(async (valid) => {
    //     if (valid) {
    //       const res = await httpWalletBind(this.form);
    //       this.is_wallet = res.data?.is_wallet || 0;
    //       if (this.is_wallet == 1) {
    //         this.getBalance();
    //         this.getRecord();
    //         // this.getAddress();
    //       }
    //     } else {
    //       console.log("error submit!!");
    //       return false;
    //     }
    //   });
    // },
  },
};
</script>
<style lang="less" scoped>
.box {
  position: relative;
  width: 100%;
  min-height: 400px;
}
.loading-content {
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
.right-top {
  width: 100%;
  height: 80px;
  background: #fff;
  border-radius: 10px;
  display: flex;
  justify-content: start;
  align-items: center;
  padding: 0 20px;
  .money {
    color: #efcd2d;
    margin: 0 10px;
  }
  .el-icon-refresh {
    cursor: pointer;
  }
}
.paymentBox {
  width: 100%;
  //   height: 260px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: flex-start;
  background: #fff;
  border-radius: 10px;
  .moneyTitle {
    font-size: 28px;
  }
  .money {
    width: 100%;
    margin-top: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .moneyNum {
      font-size: 24px;
      span {
        margin-right: 5px;
      }
    }
    .moneyBtn {
      background-color: #513216;
      border-color: #513216;
    }
  }
  .moneyInfo {
    width: 100%;
    margin-top: 10px;
    display: flex;
    justify-content: flex-start;
    align-items: center;
    font-size: 20px;
    .moneyInfoItem {
      margin-right: 20px;
    }
  }
  //   .qr-box {
  //     width: 200px;
  //     .qr-img {
  //       width: 100%;
  //       margin: 0 auto;
  //       display: flex;
  //       justify-content: center;
  //     }
  //   }
  //   .info {
  //     flex: 1;
  //     height: 100%;
  //     background: #f4f6f9;
  //     margin-left: 20px;
  //     padding: 20px;
  //     display: flex;
  //     flex-direction: column;
  //     justify-content: start;
  //     align-items: start;
  //     position: relative;
  //     .tips {
  //       width: 100%;
  //       position: absolute;
  //       top: 0;
  //       left: 0;
  //     }
  //     .info-item {
  //       margin-top: 40px;
  //       width: 100%;
  //       display: flex;
  //       justify-content: space-between;
  //       align-items: center;
  //       .rowLabel {
  //         display: inline-block;
  //         min-width: 130px;
  //         font-size: 14px;
  //       }
  //       .rowValue {
  //         margin-left: 10px;
  //         display: flex;
  //         justify-content: start;
  //         align-items: center;
  //         font-size: 14px;
  //       }
  //     }
  //     .copyBtn {
  //       display: flex;
  //       justify-content: center;
  //       align-items: center;
  //       font-size: 14px;
  //       width: 100%;
  //       margin-top: 34px;
  //       .textBtn {
  //         display: flex;
  //         justify-content: center;
  //         align-items: center;
  //         width: 180px;
  //         height: 40px;
  //         line-height: 20px;
  //         background-color: #513216;
  //         border-radius: 5px;
  //         font-size: 12px;
  //         color: #fff;
  //         margin-left: 5px;
  //         text-align: center;
  //         cursor: pointer;
  //       }
  //     }
  //   }
}
.tableBtn {
  border-radius: 20px;
}
.record {
  width: 100%;
  height: 462px;
  padding: 20px;
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  justify-content: start;
  align-items: start;
  background: #fff;
  border-radius: 10px;
  .recordTop {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    .more {
      cursor: pointer;
      color: #513216;
    }
  }
  .el-table {
    max-height: 400px;
    overflow-y: scroll;
  }
}
.address-box {
  padding: 25px !important;
}
.pay-way-box {
  margin: 30px;
  .pay-way {
    // background: #ced4f8;
    background-color: #f9f3dd;
    // display: flex;
    justify-content: space-between;
    padding: 30px;
    border-radius: 15px;
    margin-bottom: 20px;
    .top {
      margin-bottom: 20px;
    }
    .bottom {
      margin-top: 20px;
    }
    .pay-item {
      display: flex;
      justify-content: space-between;
      height: 100%;
      .address {
        font-size: 24px;
      }
    }
  }
}
.btn {
  margin: 0 auto;
  margin-top: 50px;
  width: 682px;
  height: 76px;
  background: #f9f3dd;
  font-size: 28px;
  font-weight: 400;
  letter-spacing: 0px;
  // color: rgba(255, 255, 255, 1);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 20px;
  // transition: all 0.05s;
  user-select: none;
}
.green {
  color: green;
}
.red {
  color: red;
}
.noWallet {
  background-color: #fff;
  height: 100%;
  width: 100%;
  .title {
    padding: 20px 20px 0;
  }
  .content {
    padding: 0 20px 20px;
    background-color: #fff;
  }
  .openWalletBtn {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    margin-top: 40px;
    .openWallet {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 180px;
      height: 40px;
      line-height: 20px;
      background-color: #513216;
      border-radius: 5px;
      font-size: 12px;
      color: #fff;
      margin-left: 5px;
      text-align: center;
      cursor: pointer;
    }
  }
}
.el-alert--warning {
  color: #f35757;
}
</style>
